package com.htfyun.ft.common;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;

import com.htfyun.ft.EngineerActivity;
import com.htfyun.ft.FTActivity;
import com.htfyun.ft.MainActivity;
//import com.htfyun.ft.FTFloatWindowService;
import com.htfyun.ft.FTApplication;
import com.htfyun.ft.FTFloatWindowService;
import com.htfyun.ft.agingtest.AgingTestActivity;
import com.htfyun.ft.agingtest.AgingBooleanSettingsAttr;
import com.htfyun.ft.agingtest.AgingTestItemAttr;
import com.htfyun.ft.config.FTBuildConfig;
import com.htfyun.ft.config.NVRomKeysAttr;
import com.htfyun.ft.fragment.factory.FactoryTestItemsEnum;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.hardware.NVRomController;

import java.io.File;
import java.util.List;


/**
 * Created by sst on 2018/9/6.
 */

public class FTUtils {

    private static final String FT_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "htfyunFT";

    public static String getFTStorageDirectory() {

        File file = new File(FT_PATH);
        if (!file.exists()) {
            file.mkdirs();
        }

        return file.getAbsolutePath();
    }


    public static boolean isPCChecked() {
        String result = NVRomController.getInstance().getNVRomString(NVRomKeysAttr.pc_checked.getKey());
        return result != null && result.equalsIgnoreCase(NVRomKeysAttr.PC_CHECKED);
    }

    public static boolean isFTPassed() {
        return isFactoryFTPassed();
    }

    /////////////////////////////////

    private static boolean isFactoryFTPassed() {

        return isFactoryTestItemsAllPassed();
    }

    public static boolean isFactoryTestItemsAllPassed() {
        boolean allOk = true;
        for (FactoryTestItemsEnum item : FactoryTestItemsEnum.values()) {

            if (!item.isPassed()) {
                allOk = false;
                break;
            }
        }

        return allOk;

    }

    public static boolean isFactoryTestItemsAllPassedExcludeNoFragment() {
        boolean allOk = true;
        for (FactoryTestItemsEnum item : FactoryTestItemsEnum.values()) {

            if (!item.isPassed() && item.getItemFragment() != null) {
                allOk = false;
                break;
            }
        }

        return allOk;

    }

    //////////////////////////////////////////
    public static void startFTFloat() {
        final Context context = FTApplication.getContext();

        boolean canStartOverlays = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !FTBuildConfig.CanDrawOverlays) {
            if (!Settings.canDrawOverlays(context)) {
                //启动Activity让用户授权

                canStartOverlays = false;

                Intent it = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                it.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivity(it);
            }
        }

        if (canStartOverlays) {
            context.startService(new Intent(context, FTFloatWindowService.class));
        }
    }

    public static void stopFTFloat() {
        final Context context = FTApplication.getContext();
        context.stopService(new Intent(context, FTFloatWindowService.class));
    }

    private static void startHome() {
        final Context context = FTApplication.getContext();

        try {
            Intent intent = new Intent();
            // 为Intent设置Action、Category属性
            intent.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
            intent.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void startDefaultHome() {
        final Context context = FTApplication.getContext();

        ComponentName comp = ComponentName.unflattenFromString(FTBuildConfig.DEFAULT_LAUNCHER);
        Intent intent = new Intent();
        intent.setComponent(comp);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

        if (list.size() <= 0) {
            startHome();
            return;
        }
        context.startActivity(intent);
    }

    public static void startMainActivity() {
        FTUtils.startActivity(FTApplication.getContext(), MainActivity.class);
    }

    public static void startFTActivity() {
        FTUtils.startActivity(FTApplication.getContext(), FTActivity.class);
    }

    public static void startAgingActivityAfterReboot() {

        FTUtils.startActivity(FTApplication.getContext(), AgingTestActivity.class);

    }

    public static void startAgingActivityNormal() {
        AgingBooleanSettingsAttr.no_show_tips.set(false);
        AgingBooleanSettingsAttr.keep_aging_after_reboot.set(true);
        FTUtils.startActivity(FTApplication.getContext(), AgingTestActivity.class);
    }

    //桑迪那边测试老化, 只需要测试硬件即可
    public static void startAgingActivityForSunty() {
        AgingBooleanSettingsAttr.no_show_tips.set(true);
        AgingBooleanSettingsAttr.keep_aging_after_reboot.set(false);

        AgingTestItemAttr.emmc_cpu.setForceTestOver(true);
        AgingTestItemAttr.reboot.setForceTestOver(true);
        AgingTestItemAttr.sleep.setForceTestOver(true);
        AgingTestItemAttr.hardware.setForceTestOver(false);
        AgingTestItemAttr.hardware.setTimesOfTestOver(FTBuildConfig.SUNTY_HW_AGING_TIME_MS);

        FTUtils.startActivity(FTApplication.getContext(), AgingTestActivity.class);
    }

    public static void startEngineerActivity() {
        FTUtils.startActivity(FTApplication.getContext(), EngineerActivity.class);
    }

    public static void startActivity(Context context, Class<? extends Activity> cls) {
        Intent intent = new Intent(context, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static boolean isInAgingTest() {
        return AgingBooleanSettingsAttr.aging_enabled.get();
    }

    public static boolean isKeepAgingAfterReboot() {
        return isInAgingTest() && AgingBooleanSettingsAttr.keep_aging_after_reboot.get();
    }
}
